package com.etc.zzysys.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.zzsys.entiy.AnswerBean;
import com.etc.zzsys.service.AnswerService;
import com.etc.zzsys.service.impl.AnswerServiceImpl;
import com.etc.zzsys.util.PageData;

/**
 * Servlet implementation class SingleQuestionAnswerServlet
 */
@WebServlet("/sas.do")
public class SingleQuestionAnswerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private AnswerService as = new AnswerServiceImpl();
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SingleQuestionAnswerServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String op = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		
		int page =1;//默认第一页
		int pageSize = 10;//默认每页显示10条
		//如果用户传递的参数不为空
		if(request.getParameter("page")!=null)
		{
			page = Integer.parseInt(request.getParameter("page"));
		}
		
		if(request.getParameter("pageSize")!=null)
		{
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		
		//增加了模糊查询的部分;
		String keywords="";
		if(request.getParameter("keywords")!=null)
		{
			keywords = request.getParameter("keywords");
		}
		
		int qid=0;
		if(request.getParameter("qid")!=null)
		{
			qid = Integer.parseInt(request.getParameter("qid"));
		}
		
		//if(op.equals(""))
		if("".equals(op))
		{
			
			//之前的代码要变
			PageData<AnswerBean> pd=as.queryAnswersByPage(qid,page, pageSize,keywords);
			
			 
			request.setAttribute("pd", pd);
			//需要将每次模糊查询的关键字传递回来给jsp
			request.setAttribute("keywords", keywords);
			// 从当前控制器跳转到jsp页面[问题列表]，跳转的方法叫做转发
			request.getRequestDispatcher("single.jsp").forward(request, response);
			
			as.queryAnswersByPage(qid, page, pageSize, keywords);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
